//
//  CaseManagerViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "CaseManagerViewController.h"
#import "AddCaseTableViewController.h"
#import "CaseModel.h"
#import "MainTableViewCell1.h"
#import "MainTableViewCell2.h"
#import <SVProgressHUD.h>

#define kFilePath ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"CaseData.data"])

static NSString * identifier1 = @"cell1";
static NSString * identifier2 = @"cell2";


@interface CaseManagerViewController ()<UITableViewDelegate,UITableViewDataSource,MainTableViewCell1Delegate,MainTableViewCell2Delegate>

@property(nonatomic,strong)NSMutableArray *mArray;

@property (weak, nonatomic) UITableView *contentView;


@end

@implementation CaseManagerViewController
- (NSMutableArray *)mArray {
    if (nil == _mArray) {
        _mArray = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
        NSLog(@"%@",kFilePath);
        if (_mArray.count == 0) {
            _mArray = [NSMutableArray array];
            
        }
    }
    return _mArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIView * upView =[[UIView alloc]init];
    [self.view addSubview:upView];
    upView.translatesAutoresizingMaskIntoConstraints =NO;
    
    [upView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).offset(0);
        make.height.equalTo(@150);
    }];
    
    
    UIImageView *photoImageView =[[UIImageView alloc]init];
    [self.view addSubview:photoImageView];
    [photoImageView setImage:[UIImage imageNamed:@"name"]];
    photoImageView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(upView.mas_top).offset(20);
        make.left.equalTo(upView.mas_left).offset(20);
        make.height.mas_equalTo(100);
        make.width.mas_equalTo(100);
    }];
    
    
    UIView *lineView = [[UIView alloc]init];
    [self.view addSubview:lineView];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.translatesAutoresizingMaskIntoConstraints =NO;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(upView.mas_bottom).offset(1);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.height.equalTo(@1);
        
    }];

    UIButton *sexButton = [[UIButton alloc]init];
    [self.view addSubview:sexButton];
    [sexButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(upView.mas_top).offset(20);
        make.left.equalTo(photoImageView.mas_right).offset(20);
        
    }];
    sexButton.userInteractionEnabled = NO;
    [sexButton sizeToFit];
    [sexButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sexButton setTitle:@"  男      20" forState:UIControlStateNormal];
    [sexButton setImage:[UIImage imageNamed:@"0"] forState:UIControlStateNormal];
    [sexButton setFont:[UIFont systemFontOfSize:16]];
    sexButton.enabled = NO;
    UIButton *IDNumber = [[UIButton alloc]init];
    [self.view addSubview:IDNumber];
    [IDNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sexButton.mas_bottom).offset(10);
        make.left.equalTo(photoImageView.mas_right).offset(20);
    }];
    [IDNumber sizeToFit];
    [IDNumber setTitle:@"  12341234123123" forState:UIControlStateNormal];
    [IDNumber setFont:[UIFont systemFontOfSize:16]];
    [IDNumber setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [IDNumber setImage:[UIImage imageNamed:@"idCard"] forState:UIControlStateNormal];
    IDNumber.enabled = NO;
    
    UIButton *phoneButton = [[UIButton alloc]init];
    [self.view addSubview:phoneButton];
    [phoneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(IDNumber.mas_bottom).offset(10);
        make.left.equalTo(photoImageView.mas_right).offset(20);
//        make.height.equalTo(@30);

    }];
    [phoneButton sizeToFit];
    [phoneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [phoneButton setTitle:@"  12345678901" forState:UIControlStateNormal];
    [phoneButton setImage:[UIImage imageNamed:@"phone"] forState:UIControlStateNormal];
    [phoneButton setFont:[UIFont systemFontOfSize:16]];
    phoneButton.enabled =NO;
    
    UIButton *cityButton = [[UIButton alloc]init];
    [self.view addSubview:cityButton];
    [cityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneButton.mas_bottom).offset(10);
        make.left.equalTo(photoImageView.mas_right).offset(20);
//        make.height.equalTo(@30);
        
    }];
    [cityButton sizeToFit];
    [cityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cityButton setTitle:@"  上海" forState:UIControlStateNormal];
    [cityButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateNormal];
    [cityButton setFont:[UIFont systemFontOfSize:16]];
    cityButton.enabled=NO;
    
    UILabel *nameLabel = [[UILabel alloc]init];
    [self.view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(photoImageView.mas_bottom).offset(5);
        make.left.equalTo(photoImageView).offset(0);
        make.width.equalTo(photoImageView.mas_width);
    }];
    nameLabel.text = @"病人";
    nameLabel.textAlignment =NSTextAlignmentCenter;
    
    
    UIButton *addCaseButton= [[UIButton alloc]init];
    [self.view addSubview:addCaseButton];
    [addCaseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view).offset(-50);
        make.left.equalTo(self.view).offset(50);
        make.bottom.equalTo(self.view).offset(-15);
        make.height.equalTo(@40);
    }];
    [addCaseButton setTitle:@"增加病例" forState:UIControlStateNormal];

    addCaseButton.layer.cornerRadius = 5.0f;
    
    addCaseButton.backgroundColor= [UIColor colorWithRed:49.0/255 green:199.0/255 blue:198.0/255 alpha:1];
    [addCaseButton.layer setMasksToBounds:YES];
    [addCaseButton addTarget:self action:@selector(addCaseButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UITableView * caseListTabelView = [[UITableView alloc]init];
    [self.view addSubview:caseListTabelView];
    [caseListTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView).offset(5);
        make.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(addCaseButton.mas_top).offset(-5);
    }];

    
    
    
    self.contentView = caseListTabelView;
    caseListTabelView.delegate = self;
    caseListTabelView.dataSource =self;
    [caseListTabelView registerClass:[MainTableViewCell1 class] forCellReuseIdentifier:identifier1];
    [caseListTabelView registerClass:[MainTableViewCell2 class] forCellReuseIdentifier:identifier2];

    caseListTabelView.estimatedRowHeight = 100;
    caseListTabelView.rowHeight = UITableViewAutomaticDimension;
    
    [caseListTabelView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    
}


-(void)addCaseButtonClick{
    AddCaseTableViewController *view =[[AddCaseTableViewController alloc]init];
    
    view.caseListBlock=^(CaseModel *model){
        


        [self.mArray insertObject:model atIndex:0];
        NSLog(@"%@",model.illnessType);
        NSLog(@"%@",model.photoImage);
        
        [NSKeyedArchiver archiveRootObject:self.mArray toFile:kFilePath];
//        NSLog(@"%@",kFilePath);
        [self.contentView reloadData];

        
//        [self.mArray addObject:dic];
//        NSLog(@"%@",model);

        
    };
    
    
    
    
    [self.navigationController pushViewController:view animated:YES];
    
    
}
-(void)TableClick{
    
    NSLog(@"点击了");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CaseModel *model = self.mArray[indexPath.row];
    
    MainTableViewCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:identifier1 forIndexPath:indexPath];
    cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    cell1.delegate =self;
    cell1.model = model;
    if (model.photoImage) {
        MainTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:identifier2 forIndexPath:indexPath];
        cell2.delegate = self;
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;

        cell2.model = model;
        return cell2;
        
    }
    
    return cell1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(void)cover{

    [SVProgressHUD showInfoWithStatus:@"正在开发中"];
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
    
}
@end
